/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.basics.client.widget;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.SimplePanel;

/**
 * print a link to an url.
 */
public class ExternalHyperLink
    extends SimplePanel
{
  /**
   * instructs the browser to display the new document in a new, unnamed window.
   */
  public static final String TARGET_BLANK = "_blank";

  /**
   * instructs the browser to display the new document in the same frame and
   * window as the hyperlink that loads it. All frames target themselves by
   * default, so this name is not very useful or necessary.
   */
  public static final String TARGET_SELF = "_self";

  /**
   * instructs the browser to remove all frames and display the new document in
   * the entire window. If there are no frames, then this name has the same
   * effect as _self. This target makes it possible to "exit" a framed document.
   */
  public static final String TARGET_TOP = "_top";

  /**
   * instructs the browser to display the new document in the parent frame of
   * the frame containing the hyperlink that loads it. If the frame has no
   * parent, then this name has the same effect as <code>TARGET_SELF</code>.
   */
  public static final String TARGET_PARENT = "_parent";

  /**
   * the target of the &lt;a&gt; element.
   */
  private String target = null;

  /**
   * the href of the &lt;a&gt; element.
   */
  private String url = null;



  /**
   * constructor.
   */
  public ExternalHyperLink()
  {
    super(DOM.createAnchor());
  }



  /**
   * constructor.
   * 
   * @param url
   *          the href of the &lt;a&gt; element
   */
  public ExternalHyperLink(String url)
  {
    this();
    setUrl(url);
  }



  /**
   * constructor.
   * 
   * @param url
   *          the href of the &lt;a&gt; element
   * @param target
   *          the target of the &lt;a&gt; element
   */
  public ExternalHyperLink(String url, String target)
  {
    this(url);
    setTarget(target);
  }



  /**
   * getter for the target property.
   * 
   * @return the target
   */
  public String getTarget()
  {
    return target;
  }



  /**
   * setter for the target property.
   * 
   * @param target
   *          the target to set
   */
  public void setTarget(String target)
  {
    this.target = target;
    if (target != null) {
      DOM.setElementAttribute(getElement(), "target", target);
    }
    else {
      DOM.removeElementAttribute(getElement(), "target");
    }
  }



  /**
   * getter for the url property.
   * 
   * @return the url
   */
  public String getUrl()
  {
    return url;
  }



  /**
   * setter for the url property.
   * 
   * @param url
   *          the url to set
   */
  public void setUrl(String url)
  {
    this.url = url;
    if (url != null) {
      DOM.setElementAttribute(getElement(), "href", url);
    }
    else {
      DOM.setElementAttribute(getElement(), "href", "javascript:;");
    }
  }

}
